package com.nit.lab.mapper.medical;

import com.nit.lab.dto.medical.PatientDTO;
import com.nit.lab.entity.medical.PatientEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface PatientMapper {

    @Select({"<script>SELECT " +
            "patient_Id AS `key`, patient_id,patient_name, patient_card, patient_state ,bed_id " +
            "FROM medical_patient where true" +
            "<if test='patientCard != null'> AND patient_card LIKE CONCAT ('%', #{patientCard}, '%') </if> " +
            "<if test='patientName != null'> AND patient_name LIKE CONCAT ('%', #{patientName}, '%') </if> " +
            "ORDER BY " +
            "<if test='sortField != null'> " +
            "${sortField} ${sortOrder}, " +
            "</if> " +
            "patient_id ASC " +
            "LIMIT #{start}, #{length} " +
            "</script>"})
    List<PatientDTO> list4Table(@Param("start") Integer start,
                                @Param("length") Integer length,
                                @Param("patientName") String patientName,
                                @Param("patientCard") String patientCard,
                                @Param("sortField") String sortField,
                                @Param("sortOrder") String sortOrder) throws Exception;

    @Select({"<script>SELECT COUNT(*) " +
            "FROM medical_patient " +
            "WHERE true " +
            "<if test='patientCard != null'> AND patient_Card LIKE CONCAT ('%', #{patientCard}, '%') </if> "+
            "<if test='patientName != null'> AND patient_name LIKE CONCAT ('%', #{patientName}, '%') </if> " +
            "</script>"})
    Long count4Table(@Param("patientName") String patientName,@Param("patientCard") String patientCard )throws Exception;

    @Insert("INSERT INTO medical_patient (" +
            "patient_name, patient_card, patient_state ,bed_id) " +
            "VALUES (#{entity.patientName}, #{entity.patientCard}, #{entity.patientState}, #{entity.bedId}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "patientId", keyColumn = "patient_id")
    Integer add(@Param("entity") PatientEntity patientEntity) throws Exception;

    @Update("UPDATE medical_patient " +
            "SET patient_name = #{Entity.patientName},  " +
            "patient_card = #{Entity.patientCard}, " +
            "patient_state = #{Entity.patientState}, " +
            "bed_id = #{Entity.bedId} " +
            "WHERE patient_id = #{Entity.patientId}")
    Integer update(@Param("Entity") PatientEntity patientEntity) throws Exception;

    @Delete("DELETE FROM medical_patient WHERE patient_id = #{patientId}")
    Integer remove(@Param("patientId") Long patientId) throws Exception;

    @Select("SELECT patient_id AS `key`, patient_id, patient_name, patient_card, patient_state ,bed_id " +
            "FROM medical_patient WHERE patient_id = #{patientId} ")
    PatientDTO getPatientById(@Param("patientId") Long patientId) throws Exception;
}
